<template>
  <div class="tags-container">
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 搜索框 -->
      <el-col :span="18">
        <el-form :inline="true">
          <!-- 标签名称搜索 -->
          <el-form-item label="标签名称">
            <el-input style="180px" v-model="requestParams.tagName"></el-input>
          </el-form-item>
          <!-- 状态搜索 -->
          <el-form-item label="状态">
            <el-select placeholder="请选择" v-model="requestParams.state">
              <el-option label="已启用" :value="1"></el-option>
              <el-option label="已禁用" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <!-- 清除按钮 -->
            <el-button @click="clear()">清除</el-button>
            <!-- 搜索按钮 -->
            <el-button @click="search()" type="primary">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <!-- 新增目录 -->
      <el-col :span="6" style="text-align: right">
        <el-button
          v-if="subject.id && subject.name"
          type="text"
          icon="el-icon-back"
          @click="$router.back()"
        >返回学科</el-button>
        <el-button
          type="success"
          @click="openDialog()"
          class="el-icon-edit"
        > 新增目录</el-button>
      </el-col>

      <!-- 消息提示 -->
      <el-alert :title="`数据一共${total}条`" type="info" show-icon :closable="false" class="record"> </el-alert>

      <!-- 标签列表表格区域 -->
      <el-table style="width: 100%" :data="tags">
        <!-- 第一列序号 -->
        <el-table-column type="index" :index="indexMethod" label="序号" width="80px"> </el-table-column>
        <!-- 第二列所属学科 -->
        <el-table-column prop="subjectName" label="所属学科" width="180px"> </el-table-column>
        <!-- 第三列标签名称 -->
        <el-table-column prop="tagName" label="标签名称" width="180px"> </el-table-column>
        <!-- 第四列创建者 -->
        <el-table-column prop="username" label="创建者" width="180px"> </el-table-column>
        <!-- 第五列创建日期 -->
        <el-table-column prop="addData" label="创建日期" width="180px">
          <template slot-scope="scope">{{
            scope.row.addDate | parseTimeByString
          }}</template>
        </el-table-column>
        <!-- 第六列状态 -->
        <el-table-column label="状态">
          <template slot-scope="scope">
            {{ scope.row.state === 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <!-- 第七列操作 -->
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-button type="text" @click="toggleState(scope.row)">{{ scope.row.state === 1 ? '禁用' : '启用' }}</el-button>
            <el-button type="text" @click="openDialog(scope.row)" :disabled="scope.row.state === 1">修改</el-button>
            <el-button type="text" @click="delTag(scope.row)" :disabled="scope.row.state === 1 || scope.row.totals > 0">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <div class="block" type="flex">
        <el-pagination
          style="margin-top: 20px; text-align: right"
          background
          layout="prev, pager, next, sizes, jumper"
          :total="total"
          :page-size="requestParams.pagesize"
          :current-page="requestParams.page"
          @current-change="pager"
          :page-sizes="[5, 10, 20, 50]"
          @size-change="handleSizeChange"
          hide-on-single-page
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 添加/修改文章弹出框 -->
    <tags-add
      ref="tagsAdd"
      :data="currTag"
      @updateList="updateList()"
    ></tags-add>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/tags'
import TagsAdd from '../components/tags-add'
export default {
  name: 'TagsList',
  components: { TagsAdd },
  data() {
    return {
      requestParams: {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      total: 0,
      tags: [],
      currTag: {}
    }
  },
  computed: {
    subject() {
      return this.$route.query || {}
    }
  },
  created() {
    this.getList()
  },
  // watch它可以用来监测Vue实例上的数据变动
  watch: {
    '$route.query': function() {
      this.getList()
    }
  },
  methods: {
    // 获取文章列表
    async getList() {
      try {
        // console.log(subject);
        if (this.subject.id) this.requestParams.subjectID = this.subject.id
        var { data } = await list(this.requestParams)
        this.tags = data.items
        this.total = data.counts
      } catch (error) {
        console.log(error)
      }
    },
    // 新增目录
    openDialog(tag = {}) {
      this.currTag = tag
      this.$nextTick(() => {
        this.$refs.tagsAdd.open()
      })
    },
    // 清除筛选条件
    clear() {
      this.requestParams = {
        tagsName: null,
        state: null,
        page: 1,
        pagesize: 10
      }
      // 清空搜索
      this.requestParams.tagsName = ''
      this.getList()
    },
    // 进行筛选
    search() {
      this.requestParams.page = 1
      this.getList()
    },

    // 切换条数
    handleSizeChange(size) {
      this.requestParams.page = 1
      this.requestParams.pagesize = size
      this.getList()
    },
    // 切换状态
    async toggleState(tag) {
      await changeState({
        id: tag.id,
        state: tag.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      tag.state = tag.state === 1 ? 0 : 1
    },
    // 提示信息
    async delTag(tag) {
      await this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(tag)
      this.$message.success('删除成功')
      this.getList()
    },
    // 切换分页
    pager(np) {
      this.requestParams.page = np
      this.getList()
    },
    // 新增|修改 后更新列表
    updateList() {
      if (!this.currTag.id) {
        this.requestParams.page = 1
      }
      this.getList()
    },
    // 解决分页序号不连续的问题
    indexMethod(index) {
      return index + 1 + (this.requestParams.page - 1) * this.requestParams.pagesize
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-card {
  margin: 20px;
}
.tags-container {
  // padding: 20px;
  margin: 10px 10px;
  .el-input__inner {
    height: 32px;
    line-height: 32px;
  }
}
.record {
  margin-bottom: 15px;
}
</style>
